
var prefix = "/car/carPriceRule";
var g_type = 1;
var g_tmp_ruleId = '';
$(function() {
	load();
    /*var widthPix = $('#ruleBody').css("height");
    var h = parseInt(widthPix.substring(0,widthPix.length-2));
    parent.setFrameHeight(h);*/
    $('.nav-tabs a[href="#month"]').tab('show');
});

function load() {
	$('#exampleTable')
			.bootstrapTable(
					{
						method : 'get', // 服务器数据的请求方式 get or post
						url : prefix + "/list", // 服务器数据的加载地址
						iconSize : 'outline',
						toolbar : '#exampleToolbar',
						striped : true, // 设置为true会有隔行变色效果
						dataType : "json", // 服务器返回的数据类型
						pagination : true, // 设置为true会在底部显示分页条
						singleSelect : false, // 设置为true将禁止多选
						pageSize : 10, // 如果设置了分页，每页数据条数
						pageNumber : 1, // 如果设置了分布，首页页码
						showColumns : false, // 是否显示内容下拉框（选择显示的列）
						sidePagination : "server", // 设置在哪里进行分页，可选值为"client" 或者 "server"
						queryParams : function(params) {
							return {
								limit: params.limit,
								offset:params.offset,
								ruleName:$('#searchName').val().trim(),
                                type:g_type
							};
						},
						columns : [

																{
									field : 'ruleName',
									title : '价格名称'
								},
																{
									field : 'type', 
									title : '类型',
									formatter : function(value, row, index) {
										if (value == '1') {
											return '<span class="label label-danger">月租</span>';
										} else if (value == '2') {
											return '<span class="label label-primary">季度租</span>';
										}else if (value == '3') {
                                            return '<span class="label label-warning">年租</span>';
                                        }else if (value == '4') {
                                            return '<span class="label label-info">临时</span>';
                                        }
									}
								},
								{
									field : 'amt',
									title : '金额(元)',
                                    formatter : function(value, row, index) {
                                        var type = row.type;
                                        if (type == '4') {
                                           return row.segMsg;
                                        } else{
                                            return row.amt;
										}
                                    }
								},
								{
									field : 'createTime', 
									title : '创建时间' 
								},
									{
										field : '',
										title : '状态',
                                        formatter : function(value, row, index) {
                                            var e = '<span class="label label-danger">已绑定</span>  ';
                                            var ruleId = '';
                                            if(g_tmp_ruleId == ''){
                                                if(g_type == 1){
                                                    ruleId = $("#month").val();
                                                }else if(g_type == 2){
                                                    ruleId = $("#season").val();
                                                }else if(g_type == 3){
                                                    ruleId = $("#year").val();
                                                }else if(g_type == 4){
                                                    ruleId = $("#temp").val();
                                                }
                                            }else{
                                                ruleId = g_tmp_ruleId;
                                            }

                                            if(row.id != ruleId){
                                                e = '<span class="label label-primary">未绑定</span>  ';
                                            }
                                            return e;
                                        }
									},
																{
									title : '操作',
									field : 'id',
									align : 'center',
									formatter : function(value, row, index) {
                                        var e = '<a class="btn btn-danger btn-sm" href="#" mce_href="#" title="解绑" onclick="unbind()">解绑</a>';
                                        var ruleId = '';
                                        if(g_tmp_ruleId == ''){
                                            if(g_type == 1){
                                                ruleId = $("#month").val();
                                            }else if(g_type == 2){
                                                ruleId = $("#season").val();
                                            }else if(g_type == 3){
                                                ruleId = $("#year").val();
                                            }else if(g_type == 4){
                                                ruleId = $("#temp").val();
                                            }
										}else{
                                             ruleId = g_tmp_ruleId;
										}

                                        if(row.id != ruleId){
                                            e = '<a class="btn btn-primary btn-sm" href="#" mce_href="#" title="绑定" onclick="bind(\''
                                                + row.id
                                                + '\')">绑定</a> ';
										}
										return e;
									}
								} ]
					});
}
function reLoad() {
	$('#exampleTable').bootstrapTable('refresh');
}
function add() {
	layer.open({
		type : 2,
		title : '增加',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : prefix + '/add' // iframe的url
	});
}
function edit(id) {
	layer.open({
		type : 2,
		title : '编辑',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : prefix + '/edit/' + id // iframe的url
	});
}
function remove(id) {
	layer.confirm('确定要删除选中的记录？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : prefix+"/remove",
			type : "post",
			data : {
				'id' : id
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					reLoad();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}

function resetPwd(id) {
}
function batchRemove() {
	var rows = $('#exampleTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
	if (rows.length == 0) {
		layer.msg("请选择要删除的数据");
		return;
	}
	layer.confirm("确认要删除选中的'" + rows.length + "'条数据吗?", {
		btn : [ '确定', '取消' ]
	// 按钮
	}, function() {
		var ids = new Array();
		// 遍历所有选择的行数据，取每条数据对应的ID
		$.each(rows, function(i, row) {
			ids[i] = row['id'];
		});
		$.ajax({
			type : 'POST',
			data : {
				"ids" : ids
			},
			url : prefix + '/batchRemove',
			success : function(r) {
				if (r.code == 0) {
					layer.msg(r.msg);
					reLoad();
				} else {
					layer.msg(r.msg);
				}
			}
		});
	}, function() {

	});
}

function clickType(type){
   g_type = type;
    g_tmp_ruleId = '';
    reLoad();
}
function unbind(){
    bind(-1);
}
function bind(ruleId){
	var operStr = "绑定";
	if(ruleId == -1){
        operStr = "解绑";
	}
    layer.confirm('确定要'+operStr+'这条规则吗？', {
        btn : [ '确定', '取消' ]
    }, function() {
        $.ajax({
            url : prefix+"/bind",
            type : "post",
            data : {
                'ruleID' : ruleId,
				'id':$("#id").val(),
				'type':g_type
            },
            success : function(r) {
                if (r.code==0) {
                    layer.msg(r.msg);
                    g_tmp_ruleId = ruleId;
                    reLoad();
                    parent.reLoad();
                }else{
                    layer.msg(r.msg);
                }
            }
        });
    })
}

function closeBind(){
    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
    parent.layer.close(index);
}